<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<#include "/admin/include/head.htm">
<script type="text/javascript" src="${base}/resource/js/TreeSelector.js"></script>
<script type="text/javascript">
	var paramNum = 0;
   	$(function(){
   		LG.ajax({
          url: base + '/admin/system/dictionary/getDictionaryByJson.jhtml',
          success: function(result){
          	var ts = new TreeSelector("parent_code");//select的id
			ts.headerKey="";
			ts.headerValue="请选择...";
			ts.defaultValue="${(parentCode)!''}";
			ts._data=result;
			//显示
			ts.createTree();
		  },
          error: function (message) {
              LG.showError(message);
          }
      });
		var mainform = $("#mainform");  
		//表单底部按钮 
    	LG.setFormDefaultBtn(f_cancel, function(){
			assemblyConfig();
			f_save_iframe(mainform, 's_dictionary', function(iframe, data){
		        if("${(decode)!''}" != ''){
		       		iframe.leftFrame.updateNode(data);
		        }else{
		       		iframe.leftFrame.addNode(data);
		        }
		        iframe.rightFrame.location.reload();
			});
		});
	});
	
	
	function addParam(obj){
		if(obj){
			for (var property in obj) {
				$('<tr id="param' + paramNum + '" class="param"><td style="text-align:center;width:50px;"><img src="${base}/resource/images/icons/silkicons/delete.png" onclick="deleteParam(' + paramNum + '); return false;"/></td>' +
				'<td><input type="text" class="formText {required: true}" value="' + property + '" /></td>' +
				'<td><input type="text" class="formText {required: true}" value="' + obj[property] + '" /></td></tr>').appendTo($("#config"));
				paramNum++;
			}
		}else{
			$('<tr id="param'+paramNum+'" class="param"><td style="text-align:center;width:50px;"><img src="${base}/resource/images/icons/silkicons/delete.png" onclick="deleteParam(' + paramNum + '); return false;"/></td>'
				+ '<td><input type="text" class="formText {required: true}"  /></td>'
				+ '<td><input type="text" class="formText {required: true}"  /></td></tr>').appendTo($("#config"));
			paramNum++;
		}
	}
	
	function deleteParam(num){
		$("#param"+num).remove();
	}

	function assemblyConfig(){
		var str = '{';
		$("#config .param").each(function(i,item){
			var key = item.children[1].children[0].value;
			var value = item.children[2].children[0].value;
			if(str.length > 1){
				str += ',';
			}
			if(key && key != ''){
				str += '"' + key + '":"' + value + '"';
			}
		});
		str += "}";
		$("#paramStr").val(str);
	}
</script>
</head>
<#assign index = 0>
<#if !decode??>
	<#assign isAdd = true />
<#else>
	<#assign isEdit = true />
</#if>
<body style="padding:10px; ">
	<div id="tabcontainer" style="margin:0; padding:0">
		<form id="mainform" name="mainform" class="validate" action="${base}/admin/system/dictionary/<#if isAdd??>save.jhtml<#else>update.jhtml</#if>" method="post" class="validate">
			<input type="hidden" name="decode" value="${(decode)!''}"/>
			<input type="hidden" name="config" id="paramStr"/>
			<table class="table">
				<tr>
					<th>字典名称：</th>
					<td>
						<input type="text" value="${(name)!''}" name="name" class="formText {required: true,maxlength :128}"  />
					</td>
				</tr>
				<tr>
					<th>父级字典：</th>
					<td>
						<select id="parent_code" name="parentCode"></select>
					</td>
				</tr>
				<tr>
					<th>字典描述：</th>
					<td colspan="2">
						<textarea name ="description" class="formArea {maxlength :200}" style="width: 455px; height: 165px;resize:none;">${(description)!''}</textarea>
					</td>
				</tr>
			</table>
			<table class="table" id="config">
				<div class="table_title">
					<img src="${base}/resource/images/communication.gif">
					<span>参数</span>
				</div>
				<tr>
					<td colspan="2">
						<input type="button" value="新增参数" onclick="addParam();"/>
					</td>
				</tr>
				<tr>
					<td style="text-align:center;width:50px;">
					</td>
					<td style="text-align:center;width:150px;">
						参数名称
					</td>
					<td style="text-align:center;width:150px;">
						参数值
					</td>
				</tr>
				<#if configDetail??>
					<#list configDetail.keys() as key>
						<#if key??>
							<tr id="param${index}" class="param">
								<td style="text-align:center;width:50px;">
									<img src="${base}/resource/images/icons/silkicons/delete.png" onclick="deleteParam('${index}'); return false;"/>
								</td>
								<td>
									<input type="text" class="formText {required: true}" value='${(key)!""}' />
								</td>
								<td>
									<input type="text" class="formText {required: true}" <#if configDetail[key]??>value='${(configDetail[key])!""}'</#if> />
								</td>
							</tr>
							<#assign index = index+1 >
						</#if>
					</#list>
				</#if>
			</table>
		</form>
	</div>
</body>
</html>